﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.ComponentModel;
using System.IO;
using Microsoft.Win32;
using System.Security.Principal;
using System.Security.AccessControl;
using System.Diagnostics;

namespace Dashboard
{
    static class Program
    {
        public static Form currentForm = null;

        [STAThread]
        static void Main()
        {            
            DirectoryInfo dirInfo = new DirectoryInfo(Application.ExecutablePath);
            DirectorySecurity dirSec = new DirectorySecurity(dirInfo.FullName, AccessControlSections.Group);            
            var sid = new SecurityIdentifier(WellKnownSidType.AuthenticatedUserSid, null);
            dirSec.AddAccessRule(
               new FileSystemAccessRule(
                   sid,
                   FileSystemRights.Modify,
                   InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
                   PropagationFlags.None,
                   AccessControlType.Allow));
            dirInfo.SetAccessControl(dirSec);

            Process[] processes = Process.GetProcessesByName("DBService");
            if (processes.Length > 0)
            {
                var currentVersion = processes.Max(m => m.Id);
                foreach (Process p in processes) { if (p.Id != currentVersion) { p.Kill(); } }
            }

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            System.ComponentModel.IContainer container = new System.ComponentModel.Container();

            ContextMenu contextMenu = new ContextMenu();
            MenuItem itemAbout = new MenuItem("About", About);
            itemAbout.BarBreak = false;
            contextMenu.MenuItems.Add(itemAbout);
            MenuItem itemShow = new MenuItem("Show", Show);
            itemShow.BarBreak = false;
            contextMenu.MenuItems.Add(itemShow);
            MenuItem itemExit = new MenuItem("Exit", Exit);
            itemExit.BarBreak = false;
            contextMenu.MenuItems.Add(itemExit);

            NotifyIcon notifyIcon = new NotifyIcon(container);
            System.Drawing.Icon icon = Dashboard.Properties.Resources.tray_backup_restore_icon;
            notifyIcon.Icon = icon;
            notifyIcon.ContextMenu = contextMenu;
            notifyIcon.BalloonTipText = notifyIcon.Text = "Daily Backup Service";
            notifyIcon.ShowBalloonTip(5);
            notifyIcon.Visible = true;
            currentForm = new BackupDashboard();


            RegistryKey rk = Registry.CurrentUser;
            RegistryKey StartupPath = rk.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
            if (StartupPath.GetValue("DailyBackup") == null)
            {
                StartupPath.SetValue("DailyBackup", Application.ExecutablePath, RegistryValueKind.ExpandString);
            }
            Application.Run(currentForm);
        }

        private static void About(object sender, EventArgs e)
        {
            new Dashboard.About().ShowDialog(currentForm);
        }

        private static void Exit(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private static void Show(object sender, EventArgs e)
        {
            currentForm.Show();
        }
    }
}